// pages/user/login/index.js
const app2 = getApp().globalData 
import Toast from '../../../miniprogram/miniprogram_npm/tdesign-miniprogram/toast/index';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userName:'',
    password:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if(wx.getStorageSync('userInfo')){
      wx.switchTab({
        url: '/pages/index/index',
      })
    }
  },
  nameInput(e){
    this.setData({
      userName:e.detail.value
    })
  },
  namepassword(e){
    this.setData({
      password:e.detail.value
    })
  },
  // 登录
  login(){
    if(this.data.userName == ''){
      Toast({
        context: this,
        selector: '#t-toast',
        message: '请输入用户名',
        icon: 'close-circle',
      });
      return
    }else if(this.data.password == ''){
      Toast({
        context: this,
        selector: '#t-toast',
        message: '请输入密码',
        icon: 'close-circle',
      });
      return
    }
    let userInfo= {
      userName:'木年',
      avatarUrl:'https://tdesign.gtimg.com/miniprogram/images/avatar1.png',
      type:1
    }
    wx.request({
      url: 'http://localhost:9090/api/user/login',
      method:'get',
      data:{
        studentId: this.data.userName,
        password: this.data.password
      },
      success:res=>{
        console.log(res);
        if(res.data.code == 200){
          wx.showToast({
            title: '登录成功！',
            icon:'none'
          })
          
          wx.setStorageSync('userInfo', res.data.data)
          wx.setStorageSync('userRank' , res.data.data.user.identity)
          console.log(res.data.data.user.identity == 1);
          if(res.data.data.user.identity == 1 ) { 
            // teacher  
            // no homework
            app2.routerList = [
              {
                "pagePath": "pages/index/index",
                "text": "首页",
                "iconPath": "/static/tabbar/index.png",
                "selectedIconPath": "/static/tabbar/index_on.png"
              },
              {
                "pagePath": "pages/user/index",
                "text": "我的",
                "iconPath": "/static/tabbar/user.png",
                "selectedIconPath": "/static/tabbar/user_on.png"
              }
            ]
          }else { 
            app2.routerList = [
              {
                "pagePath": "pages/index/index",
                "text": "首页",
                "iconPath": "/static/tabbar/index.png",
                "selectedIconPath": "/static/tabbar/index_on.png"
              },
              {
                "pagePath": "pages/index/t_class/stufent/homework/homework",
                "text": "作业",
                "iconPath": "/static/tabbar/作业本.png",
                "selectedIconPath": "/static/tabbar/作业本black.png"
              },
              {
                "pagePath": "pages/user/index",
                "text": "我的",
                "iconPath": "/static/tabbar/user.png",
                "selectedIconPath": "/static/tabbar/user_on.png"
              }
            ]
          }
          setTimeout(() => {
            wx.switchTab({
              url: '/pages/index/index',
            })
          }, 1000);
        }else{
          wx.showToast({
            title: res.data.msg,
            icon:'none'
          })
        }
      }
    })
   
  },
  // 注册
  register(){
    wx.navigateTo({
      url: '/pages/user/register/index',
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})